/**
 * All the polulative functions
 *
 * Originally written by: Jonas Strandstedt
 *
 */

/**
 * playerInitialization initializes the players
 */
void playerInitialization(PlayerObject *player1, PlayerObject *player2) {
	XYZ rotation;
	rotation.x = 0.0;
	rotation.y = 0.0;
	rotation.z = -M_PI/2;
	// player1
	initPlayerObject(player1,OS_OBJ_PATH(war_boat.obj), settingsArray[SETTINGS_PLAYER1_NAME], 30.0f, 0.035, rotation);
	addMeshTexture(player1->mesh, OS_IMG_PATH(war_boat.tga));

	
	// player2
	initPlayerObject(player2,OS_OBJ_PATH(CORVETTE_BODY.obj), settingsArray[SETTINGS_PLAYER2_NAME], 30.0f, 0.0025, rotation);
	addMeshTexture(player2->mesh, OS_IMG_PATH(CORVETTE_BODY_UVW_TEXTURED.tga));
	
	// allocates memory for X number of "parts", if trying to add more without allocating here then the program will crash
	 allocatePartMemory(player2, 3);
	
	 Location pos; 
	 pos.x=2.3f; pos.y=0.0f; pos.z=0.5f;
	// adds a static object to player2 with a relative position and angle
	addStaticObject(player2, OS_OBJ_PATH(CANON_CANON.obj), pos, 0.0f, 0.002, rotation);
	addMeshTexture(player2->parts[0].mesh, OS_IMG_PATH(CANON_UVW_TEXTURED.tga));
	// pos.x=0.0f; pos.y=-1.0f; pos.z=0.0f;
	//addStaticObject(player2, OS_OBJ_PATH(CORVETTE_RADAR.obj), pos, 0.0f, 0.0025, rotation);
	//addMeshTexture(player2->parts[1].mesh, OS_IMG_PATH(Fire_Texture.tga));
	pos.x=0.0f; pos.y=0.0f; pos.z=0.0f;
	addStaticObject(player2, OS_OBJ_PATH(CORVETTE_TORN.obj), pos, 0.0f, 0.0025, rotation);
	addMeshTexture(player2->parts[1].mesh, OS_IMG_PATH(CORVETTE_TORN_UVW_TEXTURED.tga));
	// addMeshTexture(player2->parts[0].mesh, OS_IMG_PATH(rainbow.tga));

}

void modelArrayPopulation() {
	XYZ rotation;
	rotation.x = 0.0;
	rotation.y = 0.0;
	rotation.z = 0.0;
	
	initArrays();
	addModel(OS_OBJ_PATH(box_vn.obj), 1.0, rotation);		// 0
	addModel(OS_OBJ_PATH(torus_vn.obj), 1.0, rotation);		// 1
	addModel(OS_OBJ_PATH(CANON_CUBE.obj), 0.005, rotation);	// 2
	addModel(OS_OBJ_PATH(water_small.obj), 5.0, rotation);	// 3
	addModel(OS_OBJ_PATH(sea_mine.obj), 0.005, rotation);	// 4
	addModel(OS_OBJ_PATH(LIFE_CUBE.obj), 0.005, rotation);	// 5
	addModel(OS_OBJ_PATH(ISLAND.obj), 0.05, rotation);		// 6
	addModel(OS_OBJ_PATH(VORTEX_BOMB.obj), 0.01, rotation);	// 7
	addModel(OS_OBJ_PATH(bouy.obj), 0.05, rotation);		// 8
	addModel(OS_OBJ_PATH(kula.obj), 0.05, rotation);		// 9
	addModel(OS_OBJ_PATH(BRYGGA.obj), 0.01, rotation);		// 10
	addModel(OS_OBJ_PATH(TITANIC_BOTTOM.obj), 0.005, rotation);		// 11
	addModel(OS_OBJ_PATH(TITANIC_MIDDLE.obj), 0.005, rotation);		// 12
	addModel(OS_OBJ_PATH(TITANIC_SKORSTEN.obj), 0.005, rotation);	// 13
	addModel(OS_OBJ_PATH(BANANA_BOAT_BOTTOM.obj), 0.005, rotation);	// 14
	addModel(OS_OBJ_PATH(BANANA_BOAT_MIDDLE.obj), 0.005, rotation);	// 15
	addModel(OS_OBJ_PATH(BANANA_BOAT_TOP.obj), 0.005, rotation);	// 16
	addModel(OS_OBJ_PATH(UBOAT_BODY.obj), 0.005, rotation);		// 17
	addModel(OS_OBJ_PATH(UBOAT_FENOR.obj), 0.005, rotation);	// 18
	addModel(OS_OBJ_PATH(UBOAT_TORN.obj), 0.005, rotation);		// 19
	addModel(OS_OBJ_PATH(SMALL_ISLAND.obj), 0.05, rotation);		// 20
	addModel(OS_OBJ_PATH(palmtree.obj), 0.04, rotation);		// 21

	addTexture(OS_IMG_PATH(rainbow.tga));						// 0
	addTexture(OS_IMG_PATH(water.tga));							// 1
	addTexture(OS_IMG_PATH(sea_mine.tga));						// 2
	addTexture(OS_IMG_PATH(LIFE_CUBE_UVW_TEXTURED.tga));		// 3
	addTexture(OS_IMG_PATH(ISLAND_UVW_TEXTURED2.tga));			// 4
	addTexture(OS_IMG_PATH(texture_bouy.tga));					// 5
	addTexture(OS_IMG_PATH(texturkula.tga));					// 6
	addTexture(OS_IMG_PATH(VORTEX_BOMB_UVW_TEXTURED.tga));		// 7
	addTexture(OS_IMG_PATH(WEAPON_CUBE_UVW_CANON.tga));			// 8
	addTexture(OS_IMG_PATH(WEAPON_CUBE_UVW_SEA_MINE.tga));		// 9
	addTexture(OS_IMG_PATH(WEAPON_CUBE_UVW_VORTEX_BOMB.tga));	// 10
	addTexture(OS_IMG_PATH(BRYGGA_UVW_TEXTURED.tga));			// 11
	addTexture(OS_IMG_PATH(TITANIC_BOTTOM_UVW_TEXTURED.tga));	// 12
	addTexture(OS_IMG_PATH(TITANIC_MIDDLE_UVW_TEXTURED.tga));	// 13
	addTexture(OS_IMG_PATH(TITANIC_SKORSTEN_TEXTURED.tga));		// 14
	addTexture(OS_IMG_PATH(BANANA_BOAT_UVW_BOTTOM_TEXTURED.tga));// 15
	addTexture(OS_IMG_PATH(BANANA_BOAT_MIDDLE_UVW_TEXTURED.tga));	// 16
	addTexture(OS_IMG_PATH(BANANA_BOAT_TOP_UVW_TEXTURED.tga));	// 17
	addTexture(OS_IMG_PATH(UBOAT_BODY_UVW_TEXTURED.tga));		// 18
	addTexture(OS_IMG_PATH(FENOR_UVW_TEXTURED.tga));			// 19
	addTexture(OS_IMG_PATH(UBOAT_TORN_UVW_TEXTURED.tga));		// 20
	addTexture(OS_IMG_PATH(SMALL_ISLAND_TEXTURED.tga));		// 21
	addTexture(OS_IMG_PATH(mask_palm.tga));		// 22
	
}

/**
 * objectPopulation populates the scene with collisionobjects of different kinds
 */
void objectPopulation() {

	XYZ angle;
	angle.x = 0.0f;
	angle.y = 0.0f;
	angle.z = 0.0f;
	
	Location pos; pos.z=0.0f;
	pos.x=0.0f; pos.y=0.0;
	addCollisionObject("Water", 3, 1, pos, angle, 20.0f, OBJECT_PASS_THROUGH, 0, 0, stegu3);
	//addCollisionObject("Water", 3, 1, pos, angle, 20.0f, OBJECT_PASS_THROUGH, 0, 0, waterShader);
	
	
	/**
	 * addCollisionObject(namn, index, texur, position, vinkel, massa, bump, händelse vid bump, animering) invänter en ny också. 
	 */
	//box objects
	/*
	pos.x=10.0f; pos.y=5.0f;
	addCollisionObject("Box1", 0, -1, pos, angle, 20.0f, OBJECT_BUMP, object_switcher, object_buoy, 0);
	pos.x=10.0f; pos.y=-5.0f;
	addCollisionObject("Box2", 0, -1, pos, angle, 50.0f, OBJECT_BUMP, 0, object_buoy, 0);
	pos.x=10.0f; pos.y=-10.0f;
	addCollisionObject("Box3", 0, -1, pos, angle, 50.0f, OBJECT_IMMOVABLE, 0, 0, 0);
	pos.x=10.0f; pos.y=-15.0f;
	addCollisionObject("Box4", 0, -1, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, object_eight, 0);
	*/
	// powerups
	pos.x=0.0f; pos.y=15.0f;
	addCollisionObject("powerup_canon", 2, 8, pos, angle, 50.0f, OBJECT_PASS_THROUGH, powerup_cannon, object_hover_rotate, 0);
	pos.x=0.0f; pos.y=40.0f;
	addCollisionObject("powerup_mine", 2, 9, pos, angle, 50.0f, OBJECT_PASS_THROUGH, powerup_mine, object_hover_rotate, 0);
	pos.x=0.0f; pos.y=60.0f;
	addCollisionObject("powerup_swirlbomb", 2, 10, pos, angle, 50.0f, OBJECT_PASS_THROUGH, powerup_swirlbomb, object_hover_rotate, 0);

	pos.x=0.0f; pos.y=-15.0f;
	addCollisionObject("powerup_canon", 2, 8, pos, angle, 50.0f, OBJECT_PASS_THROUGH, powerup_cannon, object_hover_rotate, 0);
	pos.x=0.0f; pos.y=-40.0f;
	addCollisionObject("powerup_mine", 2, 9, pos, angle, 50.0f, OBJECT_PASS_THROUGH, powerup_mine, object_hover_rotate, 0);
	pos.x=0.0f; pos.y=-60.0f;
	addCollisionObject("powerup_swirlbomb", 2, 10, pos, angle, 50.0f, OBJECT_PASS_THROUGH, powerup_swirlbomb, object_hover_rotate, 0);

	
	//Evil objects
	/*
	pos.x=-10.0f; pos.y=-5.0f;
	addCollisionObject("Evil1", 0, 0, pos, angle, 50.0f, OBJECT_PASS_THROUGH, player_sink, object_buoy, 0);
	pos.x=-10.0f; pos.y=5.0f;
	addCollisionObject("Evil2", 0, 0, pos, angle, 50.0f, OBJECT_PASS_THROUGH, player_whirlpool, object_hover_rotate, 0);
	pos.x=-10.0f; pos.y=10.0f;
	addCollisionObject("Evil3", 0, 0, pos, angle, 50.0f, OBJECT_PASS_THROUGH, player_explode,0,0);
	*/
	
	//Life
	pos.x=-20.0f; pos.y=10.0f;
	addCollisionObject("Life1", 5, 3, pos, angle, 50.0f, OBJECT_PASS_THROUGH, powerup_lifebox, object_buoy,0);
	pos.x=-50.0f; pos.y=10.0f; pos.z-=2.0f;
	
	//Islands
	addCollisionObject("Island1", 6, 4, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	pos.x=65.0f; pos.y=10.0f; pos.z=-2.0f; angle.y = 3.14f;
	addCollisionObject("Island2", 6, 4, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);

	pos.x=-100.0; pos.y=10.0f; pos.z=-1.5f;angle.y = 0.0f;angle.z = 7.0f+M_PI;
	addCollisionObject("Island3", 20, 21, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	pos.x=65.0f; pos.y=-70.0f; pos.z=-1.5f; angle.y = 0.0f;angle.z = M_PI;
	addCollisionObject("Island4", 20, 21, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	
	pos.x=65.0f; pos.y=-70.0f; pos.z=3.0f; angle.y = 0.0f;angle.z = M_PI;
	addCollisionObject("palmtree", 21, 22, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	
	// brygga och båtar
	pos.x=0; pos.y=-110.0f; pos.z=1.0f; angle.y = 0.0f;angle.z = 7.0f+M_PI;
	addCollisionObject("Bridge", 10, 11, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	
	pos.x=-16.0; pos.y=-110.0f; pos.z=-2.0f; angle.y = 0.0f;angle.z = 7.0f+M_PI;
	addCollisionObject("titanic", 11, 12, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	addCollisionObject("titanic", 12, 13, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	addCollisionObject("titanic", 13, 14, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	
	pos.x=12; pos.y=-110.0f; pos.z=0.6f; angle.y = 0.0f;angle.z = 7.0f+M_PI;
	addCollisionObject("banan", 14, 15, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	addCollisionObject("banan", 15, 16, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	addCollisionObject("banan", 16, 17, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	
	pos.x=0; pos.y=-140.0f; pos.z=1.6f; angle.y = 0.0f;angle.z = 7.0f;
	addCollisionObject("uboat", 17, 18, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	addCollisionObject("uboat", 18, 19, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);
	addCollisionObject("uboat", 19, 20, pos, angle, 50.0f, OBJECT_PASS_THROUGH, 0, 0,0);


    angle.y = 0.0f; pos.z=0.0f;
	addCollisionObjectBoxes();


	
}

void addCollisionObjectBoxes() {
	Location pos;
	XYZ angle;
	angle.x = 0.0f;
	angle.y = 0.0f;
	angle.z = 0.0f;

	
	int i;
	int segments = 50;
	float segment_angle = 0.0f;
	Location positions[segments];
	float radius_boundary = 200.0f;
	
	
	for (i=0; i<segments;i++) {
		segment_angle = 2.0 * M_PI * ( (float) i / (float) segments);
		angle.z = segment_angle;
		positions[i].x = cos(segment_angle)*radius_boundary;
		positions[i].y = sin(segment_angle)*radius_boundary;
		positions[i].z = 0.0;
		
		//printf("index: %i, angle: %f, %f \n",i,segment_angle, positions[i].x);
		
		addCollisionObject("Bouy", 8, 5, positions[i], angle, 50.0f, OBJECT_BUMP, 0,object_buoy,0);		
	}		


}
